package ssi.actions;

import java.sql.Connection;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionRedirect;

import ssi.beans.User;
import ssi.tools.Connector;

public class CancelFavouritesAction  extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		request.setCharacterEncoding("UTF-8");
		String target=null;
		User user=(User)form;
	
		String customerID = String.valueOf(user.getId());
		String animalID = user.getAnimalID();
		try{
			Connection connection = new Connector().getConnection();
    		Statement statement = connection.createStatement();
    		statement.executeUpdate("Delete from favourites where userId='"+customerID+"' AND animalId='"+animalID+"'");
    		target="success";
    		System.out.println("UDALO SIE");
    		connection.close();
		}catch(Exception e){
			target="failure";
			e.printStackTrace();
		} 
		ActionRedirect redirect = new ActionRedirect(mapping.findForward(target));
		redirect.addParameter("id", animalID);
		return redirect;
	}
}
